<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>垫款业务手动规则表管理</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<form id="capAdvRuleInfo_form">
    <div class="fieldset1 tabel_col tabel_col_1 clearFix">
        <div class="fieldBox col_100">
            <div class="label">垫款规则名称</div>
            <div class="field">
                <input name="ruleNm" id="ruleNm" type="text" class="ui-textbox"
                       value="${capAdvRuleInfoVO.ruleNm}" validate="{required:true}">
                <span class="requiredSymbol">*</span>
                <input type="text" style="display: none;" id="ruleTypCd" name="ruleTypCd"
                       value="${capAdvRuleInfoVO.ruleTypCd == null ? "0":capAdvRuleInfoVO.ruleTypCd}">
            </div>
        </div>
        <div class="fieldBox col_100">
            <div class="label">启用状态</div>
            <div class="field">
                <input type="radio" name="ruleStsCd" id="ruleStsCd_0"
                       <c:if test="${not empty capAdvRuleInfoVO and capAdvRuleInfoVO.ruleStsCd == '1'}">checked</c:if>/><span
                    class="requiredSymbol">开启</span>&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="radio" name="ruleStsCd" id="ruleStsCd_1"
                       <c:if test="${not empty capAdvRuleInfoVO and capAdvRuleInfoVO.ruleStsCd == '0'}">checked</c:if> /><span
                    class="requiredSymbol">关闭</span>
                <span class="requiredSymbol">*</span>
            </div>
        </div>
        <%--<div class="fieldBox col_100">
            <div class="label">垫款日期类型</div>
            <div class="field">
                <input name="dtTypCd" id="dtTypCd"
                       data-value="${capAdvRuleInfoVO.dtTypCd}" validate="{required:true}"
                       placeholder="请选择" ltype="combobox">
                <span class="requiredSymbol">*</span>
            </div>
        </div>
        <div class="fieldBox col_100">
            <div class="label">垫款周期</div>
            <div class="field">
                <input name="advCycCd" id="advCycCd"
                       data-value="${capAdvRuleInfoVO.advCycCd}" validate="{required:true}"
                       placeholder="请选择" ltype="combobox">
                <span class="requiredSymbol">*</span>
            </div>
        </div>
        <div class="fieldBox col_100">
            <div class="label">垫款时间</div>
            <div class="field">
                <input name="advTmCd" id="advTmCd"
                       data-value="${capAdvRuleInfoVO.advTmCd}" validate="{required:true,select:true}"
                       placeholder="请选择" ltype="combobox">
                <span class="requiredSymbol">*</span>
            </div>
        </div>--%>
        <div class="fieldBox col_100">
            <div class="label">垫款租金范围</div>
            <div class="field">
                <input name="advMinAmt" id="advMinAmt" type="text" class="ui-textbox"
                       data-value="${capAdvRuleInfoVO.advMinAmtStr}" validate="{number:true}">
                ~
                <input name="advMaxAmt" id="advMaxAmt" type="text" class="ui-textbox"
                       data-value="${capAdvRuleInfoVO.advMaxAmtStr}" validate="{number:true}">
                <span class="requiredSymbol">*</span>
            </div>
        </div>
        <div class="fieldBox col_100">
            <div class="label">垫款逾期天数</div>
            <div class="field">
                <input name="minOvdDay" id="minOvdDay" type="text" class="ui-textbox"
                       value="${capAdvRuleInfoVO.minOvdDay}" validate="{number:true}">
                ~
                <input name="maxOvdDay" id="maxOvdDay" type="text" class="ui-textbox"
                       value="${capAdvRuleInfoVO.maxOvdDay}" validate="{number:true}">
                <span class="requiredSymbol">*</span>
            </div>
        </div>

        <div class="fieldBox col_100">
            <div class="label">垫款期数</div>
            <div class="field">
                <input name="advPd" id="advPd" type="text" class="ui-textbox"
                       value="${capAdvRuleInfoVO.advPd}" validate="{required:true,number:true}">期
                &nbsp;&nbsp;&nbsp;&nbsp;
                <input type="checkbox" name="isAllAdv" id="isAllAdv"
                       <c:if test="${capAdvRuleInfoVO.isAllAdv == 1}">checked</c:if>>
                <span class="requiredSymbol">全垫</span>
            </div>
            <span class="requiredSymbol">*</span>
        </div>
        <div class="fieldBox col_100">
            <div class="label">垫款基数</div>
            <div class="field">
                <input name="advBaseCd" id="advBaseCd"
                       data-value="${capAdvRuleInfoVO.advBaseCd}" validate="{required:true}"
                       placeholder="请选择" ltype="combobox">
                <span class="requiredSymbol">*</span>
            </div>
            <span class="requiredSymbol">*</span>
        </div>
        <div class="fieldBox col_100">
            <div class="label">垫款方式</div>
            <div class="field">
                <input name="advTypCd" id="advTypCd"
                       data-value="${capAdvRuleInfoVO.advTypCd}" validate="{required:true}"
                       placeholder="请选择" ltype="combobox">
                <span class="requiredSymbol">*</span>
            </div>
        </div>
    </div>
    <div>
        <span id="selectBtn" class="sq-button sq-button-round l-dialog-btn-ok" onclick="f_import()">请选择项目</span>
    </div>
    <div id="ligergrid" class="sq-grid" style="display: block"></div>
    <div class="btnsBox">
        <span id="saveBtnNew" class="sq-button sq-button-round l-dialog-btn-ok">预览付款指令</span>
        <span id="cancleBtn" class="sq-button sq-button-round l-dialog-btn-ok">取消</span>
        <input type="reset" id="resetBtn" class="l-button l-button-reset Hide">
    </div>
</form>
<script>
    var ligerForm, ligerGrid = null;
    var dia1;
    function f_import(){
        dia1 = $.ligerDialog.open({ title: '选择项目', width: 700, height: 550, url: '<%=path%>/capital/capAdvRuleInfo/toFnrPrjListPage.do', buttons: [
            { text: '确定', onclick: f_importOK },
            { text: '取消', onclick: f_importCancel }
        ]
        });
    }
    function f_importOK(item, dialog)
    {
        var rows = dialog.frame.f_select();
        alert(JSON.stringify(rows));
        if (!rows)
        {
            alert('请选择行!');
            return;
        }
        var manager = $("#ligergrid").ligerGetGridManager();
        var data = manager.getData();
        var ids = [] ;
        if(data){
            for(var index in data){
                ids.push(data[index].id);
            }
        }
        if(rows){
            for (var i = 0; i < rows.length; i++)
            {
                if(!ids){
                    ligerGrid.addRow(rows[i]);
                }else if(ids.indexOf(rows[i].id) == -1){
                    ligerGrid.addRow(rows[i]);
                }
            }
        }
        dialog.close();
    }
    function f_importCancel(item, dialog)
    {
        dialog.close();
    }

    function deleteRow(val)
    {
        var manager = $("#ligergrid").ligerGetGridManager();
        //manager.deleteSelectedRow();
        manager.deleteRow(val);
    }

    $(function () {
        var capAdvRuleInfoId = "${capAdvRuleInfoVO.id}";//主键ID

        $('#isAllAdv').ligerCheckBox();
        $('#ruleStsCd_0').ligerRadio();
        $('#ruleStsCd_1').ligerRadio();
//        var form = liger.get("capAdvRuleInfo_form");
        ligerForm = $("#capAdvRuleInfo_form").ligerForm({
            validate: true
        });

        //垫款日期类型
        $("#dtTypCd").ligerComboBox({
            data: ${advDtKvList},
            valueField: 'key',
            textField: 'value'
        });
        //垫款周期
        $("#advCycCd").ligerComboBox({
            data: ${advPeriodKVList},
            valueField: 'key',
            textField: 'value'
        });
        //垫款基数
        $("#advBaseCd").ligerComboBox({
            data: ${advBaseCdKvList},
            valueField: 'key',
            textField: 'value'
        });
        //垫款方式
        $("#advTypCd").ligerComboBox({
            data: ${advWayKvList},
            valueField: 'key',
            textField: 'value'
        });

        //垫款时间
        $("#advTmCd").ligerComboBox({
            data: ${advTmKvList},
            valueField: 'key',
            textField: 'value'
        });

        //设置逾期期数验证
        <c:if test="${not empty capAdvRuleInfoVO and capAdvRuleInfoVO.isAllAdv == '1'}">
        ligerForm.setFieldValidate("advOvdPdCd", {
            required: false
        });
        liger.get("advPd").setDisabled(true);
        //$("#advPd").attr("disabled",true);
        </c:if>

        ligerGrid = $("#ligergrid").ligerGrid({
            <%--url: '<%=path%>/capital/capAdvRuleInfo/initFncPrjListPage.do', fncprjnm,crttm,crtpsnecd --%>
            url : '<%=path%>/capital/capAdvRuleInfo/toListPage.do',
            method: 'GET',
            columns: [
                {
                    display: '项目名称', name: 'ruleNm', minWidth: 100, align: 'center', enabledSort: false
                },
                {
                    display: '生效时间',
                    name: 'crtTm',
                    minWidth: 100,
                    align: 'center',
                    enabledSort: false,
                    render: renderDate
                },
                {
                    display: '添加人', name: 'crtUsrId', minWidth: 100, align: 'center', enabledSort: false,
                },
                {
                    display: '操作', minWidth: 100, align: 'center', enabledSort: false,
                    render:function(rowdata,index,value){
                        return '<a href="javascript:deleteRow('+index+');" class="link">删除</a>';
                    }
                },
            ],
            pageSize: 10,
            root: 'rows',
            record: 'total',
            width: '100%',
            height: '100%',
            heightDiff: -2,
            allowUnSelectRow: true,
            alternatingRow: true,
            rownumbers: true,
            enabledEdit: false,
            usePagerOnLessPagesize: true,
            checkbox: true,
        });
        /*垫款期数*/
        $("#isAllAdv").change(function () {
            if (this.checked) {
                liger.get("advPd").setDisabled(true);
                //$("#advPd").attr("disabled",true);
                ligerForm.setFieldValidate("advPd", {
                    required: false
                });
            } else {
                liger.get("advPd").setEnabled(true);
                //$("#advPd").attr("disabled",false);
                ligerForm.setFieldValidate("advPd", {
                    required: true
                });
            }
        });

        /*取消*/
        $("#cancleBtn").click(function(){
            frameElement.dialog.close();
        });

        /*保存*/
        $("#saveBtnNew").click(function (event) {
            if (!ligerForm.valid()) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                return false; //验证不通过，不执行以下代码
            }
            if ($("#advMinAmt").val() == '' && $("#advMaxAmt").val() == '' && $("#minOvdDay").val() == '' && $("#maxOvdDay").val() == '') {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CAP_ADV_RULE_RENK, SQ_type: "warning"});
                return false;
            }
            var data = ligerForm.getData();
            var ruleTypCd = $("#ruleTypCd").val();
            data['ruleTypCd'] = ruleTypCd;
            if (data.isAllAdv) {
                data.isAllAdv = 1;
            } else {
                data.isAllAdv = 0;
            }

            if (liger.get('ruleStsCd_0').checked) {
                data.ruleStsCd = 1;
            } else {
                data.ruleStsCd = 0;
            }
            alert("data == " + JSON.stringify(data));
            $.ligerDialog.hide();
            if (ligerGrid == null) {
                $.ligerDialog.SQalert({content: "grid为空", SQ_Typt: "warning"});
            }
            var selectedRows = ligerGrid.getData();
            alert(JSON.stringify(selectedRows));
            var _ids = "";
            if (selectedRows && selectedRows.length > 0) {
                for (var index in selectedRows) {
                    if(selectedRows.length-1 == index){
                        _ids += selectedRows[index].id;
                    }else{
                        _ids += selectedRows[index].id +",";
                    }
                    //_ids.push(selectedRows[index].id);
                }
            }else{
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_MORE_ROW, SQ_type: "warning"});
                return;
            }
            alert("_ids == " + JSON.stringify(_ids));
//            var _url = '<%=path%>/capital/capAdvRuleInfo/addManualRuleInfo.do';
            var wait_dialog;
            $.ajax({
                type:'post',
                dataType:'json',
                data: {ids: _ids, dataList: JSON.stringify(data)},
                async: false,
                url: '<%=path%>/capital/capAdvRuleInfo/addManualRuleInfo.do',
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                        parent.$("#clearBtn").click();
                        parent.$("#searchBtn").click();
                        frameElement.dialog.close();

                        //$("#resetBtn").click();
                        //回调刷新列表
                        //var callback=frameElement.W2W.callback;
                        //Remove_navTab();
                        //callback();
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                },
                failure:function(){
                    alert("error");
                }
            });
        });
    });

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }

    /* new保存接口 2017-12-8 12:29:30 jiangzg*/
    $("#aaa").click(function (event) {
        if (!ligerForm.valid()) {
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
            return false; //验证不通过，不执行以下代码
        }
        if ($("#advMinAmt").val() == '' && $("#advMaxAmt").val() == '' && $("#minOvdDay").val() == '' && $("#maxOvdDay").val() == '') {
            $.ligerDialog.SQalert({content: LogUtils.LOG_CAP_ADV_RULE_RENK, SQ_type: "warning"});
            return false;
        }
        var data = ligerForm.getData();
        var ruleTypCd = $("#ruleTypCd").val();
        data['ruleTypCd'] = ruleTypCd;
        alert("data == " + JSON.stringify(data));
        if (data.isAllAdv) {
            data.isAllAdv = 1;
        } else {
            data.isAllAdv = 0;
        }

        if (liger.get('ruleStsCd_0').checked) {
            data.ruleStsCd = 1;
        } else {
            data.ruleStsCd = 0;
        }
        $.ligerDialog.hide();
        if (ligerGrid == null) {
            return;
        }
        var selectedRows = ligerGrid.getSelectedRows();
        if (selectedRows.length == 0) {
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warning"});
            return;
        }
        var _id = selectedRows[0].id;
        $.ligerDialog.open({
            width: 650,
            height: 600,
            title: '修改垫款业务规则表',
            url: '<%=path%>/capital/capAdvRuleInfo/toModifyPage.do?id=' + _id
        });
        //Add_navTab('CapAdvRuleInfo_2'+_id, "修改垫款业务规则表", '<%=path%>/capital/capAdvRuleInfo/toModifyPage.do?id='+_id, callback);
    });
</script>
</body>
</html>
